package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.PsFunctionMapper;
import com.maochang.mapper.PsInspectiontaskDetailMapper;
import com.maochang.mapper.PsRoleMapper;
import com.maochang.mapper.PsRolePremissionMapper;
import com.maochang.pojo.PsFunction;
import com.maochang.pojo.PsInspectiontaskDetail;
import com.maochang.pojo.PsRolePremission;
import com.maochang.pojo.util.TreeNode;
import com.maochang.service.PsFunctionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static java.time.ZonedDateTime.now;

@Service("PsFunctionServiceImpl")
public class PsFunctionServiceImpl extends ServiceImpl<PsFunctionMapper, PsFunction> implements PsFunctionService {

    @Resource
    private PsRoleMapper psRoleMapper;
    @Resource
    private PsFunctionMapper psFunctionMapper;
    @Resource
    private PsRolePremissionMapper psRolePremissionMapper;
    @Override
    public void grant(Integer roleId,String[] functionid) {
        psRolePremissionMapper.delete(Wrappers.<PsRolePremission>update().eq("roleId",roleId));
        if(functionid!=null&&functionid.length>0){
            for(String id:functionid){
                psRolePremissionMapper.insert(new PsRolePremission(null,roleId,id, LocalDateTime.now(),"光头强",LocalDateTime.now(),1));
            }
        }
    }



    private List<TreeNode> searchByParentId(Integer parentId,Integer roleId){
            LambdaQueryWrapper<PsFunction> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(PsFunction::getParentId,parentId);
            List<PsFunction> list=psFunctionMapper.selectList(wrapper);
        List<Integer> permIdList=psRolePremissionMapper.findPermIdList(roleId);
            return list.stream().map(psFunction -> {
                TreeNode treeNode=new TreeNode();
                treeNode.setId(psFunction.getIdb());
                treeNode.setTitle(psFunction.getTitle());
                int count=psFunctionMapper.selectCount(Wrappers.<PsFunction>query().eq("parentId",psFunction.getId()));
                if(permIdList!=null&&permIdList.contains(psFunction.getIdb())){
                    treeNode.setChecked(true);
                }
                return treeNode;
            }).collect(Collectors.toList());
    }

    @Override
    public List<TreeNode> searchTreeData(Integer roleId) {
        List<TreeNode> menuList01=this.searchByParentId(0,roleId);
        for (TreeNode node01:menuList01){
            List<TreeNode> menuList02=this.searchByParentId(node01.getId(),roleId);
            node01.setChildren(menuList02);
        }
        return menuList01;
    }
}
